package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Zkl;
import factory.ServiceFactory;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class Zk
 */
@WebServlet("/zk.do")
public class Zk extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Zk() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String meth = request.getParameter("method");
		if (meth == null || meth.equals("")) {
			meth = "index";
		}
		switch (meth) {
		case "index":
			request.getRequestDispatcher("addZk.jsp").forward(request, response);
			break;
		case "addZk":
			addZk(request, response);
			break;
		case "changeZk":
			changeZkl(request, response);
			break;
		default:
			break;
		}
	}
	
	protected void changeZkl(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		String title = request.getParameter("title");
		String zkString = request.getParameter("zk");
		float zk = Float.valueOf(zkString == null ? "0" : zkString);
		String idString = request.getParameter("id");
		int id = Integer.valueOf(idString == null ? "0" : idString);
		if (title == null || title.equals("") || title.length() > 50 || zk <= 0 || zk > 1 || id == 0) {
			json.put("status", 500);
			json.put("msg", "非法的折扣类！");
		}else {
			Zkl zkl = new Zkl(title, zk);
			zkl.setId(id);
			ServiceFactory.getSerInstance().updateZkl(zkl);
			json.put("status", 200);
			json.put("msg", "修改成功！");
		}
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

	protected void addZk(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		String title = request.getParameter("title");
		String zkString = request.getParameter("zk");
		float zk = Float.valueOf(zkString == null ? "0" : zkString);
		if (title == null || title.equals("") || title.length() > 50 || zk <= 0 || zk > 1) {
			json.put("status", 500);
			json.put("msg", "非法的折扣类！");
		} else {
			Zkl zkl = new Zkl(title, zk);
			int zklId = ServiceFactory.getSerInstance().addZkl(zkl);
			zkl.setId(zklId);
			json.put("status", 200);
			json.put("id", zklId);
		}
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

}
